package com.example.springbootredis.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.springbootredis.entity.User;
import com.example.springbootredis.util.BaseResp;
import com.example.springbootredis.util.TestDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Set;


@Slf4j
@Api("redis【lua】")
@RestController
@RequestMapping("/redis/lua")
public class RedisLuaController {

    @Resource
    private DefaultRedisScript<Long> limitScript;

    @Resource
    private DefaultRedisScript<String> mobileScript;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation("发送短信-lua脚本")
    @GetMapping(value = "/sendSMS")
    public String sendSMS(HttpServletRequest request) {
        String mobile = "19999999999";
        String limit_time = "5";
        List<String> keys = Arrays.asList("mobile:" + mobile);
        String resp = this.stringRedisTemplate.execute(this.mobileScript, keys, "1","EX",limit_time,"NX");
        log.info("resp={}", resp);
        if("OK".equals(resp)){
            return "发送成功";
        }
        return "每"+limit_time+"秒只能发送一条短信";
    }


}
